﻿using ERPU8WebApi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ERPU8WebApi.Until
{
    public class PubUntil
    {
        public static int toInt(object value)
        {
            try
            {
                if (value == null)
                {
                    return 0;
                }
                switch (value.GetType().ToString())
                {
                    case "System.String":
                        return Int32.Parse((string)value);
                    case "System.Int64":
                        return Int32.Parse(((long)value).ToString());
                    case "System.Int32":
                        return (int)value;
                    case "System.Double":
                        return (int)System.Math.Round((double)value, 0);
                    case "System.Boolean":
                        return 0;
                    case "System.DateTime":
                        return 0;
                    default:
                        return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
        public static double toDouble(object value)
        {
            try
            {
                if (value == null)
                {
                    return 0.0;
                }
                switch (value.GetType().ToString())
                {
                    case "System.String":
                        return Double.Parse((string)value);
                    case "System.Int64":
                        return Double.Parse(((long)value).ToString());
                    case "System.Int32":
                        return Double.Parse(((int)value).ToString());
                    case "System.Boolean":
                        return 0.0;
                    case "System.Double":
                        return (int)System.Math.Round((double)value, 0);
                    case "System.DateTime":
                        return 0.0;
                    default:
                        return 0;
                }
            }
            catch
            {
                return 0.0;
            }
        }
        public static DateTime toDateTime(object value)
        {
            try
            {
                if (value == null)
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return Convert.ToDateTime(value);
                }
              
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
        }

        public static ResponseModel CallWebApi(string url, RequestModel rm)
        {
            string jsonConsignment = JsonConvert.SerializeObject(rm);
            string addConsignmentResult = HttpHelper.HttpPost(url, jsonConsignment);
            ResponseModel rms = JsonConvert.DeserializeObject<ResponseModel>(addConsignmentResult);
            return rms;
        }
    }
}